#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/cdev.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/mutex.h>
#include <linux/interrupt.h>
#include <linux/bug.h>			/* For BUG_ON.  */
#include <linux/cpu.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */
#include <linux/delay.h>
#include <linux/smp.h>
#include <linux/kernel_stat.h>
#include <linux/sched.h>
#include <linux/percpu-defs.h>
#include <linux/wait.h>
#include <linux/gpio/driver.h>
#include <linux/atomic.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/kfifo.h>

#define MY_MAX_MINORS  1
#define   DEVICE_NAME  "device name 500"

#define GPIO_1_BASE  0xFF730000
#define  GPIO1_SWPORTA_DR GPIO_1_BASE
#define  GPIO1_SWPORTA_DDR  0xFF730004

#define  GPIO_INTEN   (GPIO_1_BASE + 0x0030)
#define  GPIO_PIN  39
#define LOCATION  7

DEFINE_KFIFO(myfifo, char, 1024);

struct my_device_data {
	struct cdev cdev;
};

DECLARE_WAIT_QUEUE_HEAD(wq);

static struct my_device_data devs[MY_MAX_MINORS];

static int my_open(struct inode *inode, struct file *file) {
	pr_info("a3 my_open \n");
	return 0;
}

static ssize_t my_write(struct file *filp, const char __user *user_buffer,
		size_t size, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("write");
	ret = kfifo_from_user(&myfifo, user_buffer, size, &len);
	if (ret != 0) {
		pr_err("kfifo_from_user error");
		return 0;
	}
	if (len <= 0)
		return 0;
	*offset += len;
	wake_up(&wq);
	return len;
}

static ssize_t my_read(struct file *filp, char __user *user_buffer,
		size_t count, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("read");
	ret = kfifo_to_user(&myfifo, user_buffer, count, &len);
	if (len <= 0)
		return 0;
	*offset += len;
	return len;
}

unsigned int my_poll(struct file *flip, struct poll_table_struct *table) {
	int mask = 0;
	pr_info("my_poll \n");
	poll_wait(flip, &wq, table);
	if (kfifo_is_empty(&myfifo)) {

	} else {
		mask |= POLLIN | POLLRDNORM;
	}
	return mask;
}

int my_close(struct inode *inode, struct file *flip) {
	pr_info("my_close \n");
	return 0;
}

struct fasync_struct *fasync_struct;

int my_fasync(int fd, struct file *flip, int on) {
	return fasync_helper(fd, flip, on, &fasync_struct);
}

static const struct file_operations my_fops = { .owner = THIS_MODULE, .open =
		my_open, .read = my_read, .write = my_write, .poll = my_poll, .release =
		my_close,
		.fasync = my_fasync,
};

static dev_t mydev;

irqreturn_t key_handler(int irq, void *dev_id) {
	pr_err("key_handler \n");
	kill_fasync(&fasync_struct, SIGIO, POLL_OUT);
	pr_err("key_handler 2 \n");
	return IRQ_HANDLED;
}

unsigned int num;

static __init int my_init(void) {
	int i;
	int err;
	int ret;

	unsigned int __iomem *direction = ioremap(GPIO1_SWPORTA_DDR, 4);
			unsigned int a = readl(direction);
			unsigned int __iomem *irq = ioremap(GPIO_INTEN, 4);


	pr_info("a3 init_module\n");err = alloc_chrdev_region(&mydev, 0, MY_MAX_MINORS, DEVICE_NAME);
	if (err != 0) {
		return err;
	}

	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* initialize devs[i] fields */
		cdev_init(&devs[i].cdev, &my_fops);
		cdev_add(&devs[i].cdev, MKDEV(MAJOR(mydev), i), 1);
	}

	// set direction in
		writel(readl(direction) & (~(1 << LOCATION)), direction);
		//just check
		a = readl(direction);
		pr_err("direction: %d \n", a);

		pr_err("direction 1 or 0  ? : %d \n", (a & (1 << LOCATION)) >> LOCATION);

		// enable Interrupt
		writel(readl(irq) | (1 << LOCATION), irq);

		pr_err("Interrupt 1 or 0  ? : %d \n",
		(readl(irq) & (1 << LOCATION)) >> LOCATION);

		num = gpio_to_irq(GPIO_PIN);
		pr_info("irq_num:%d\n", num);ret = request_irq(num, key_handler, IRQF_TRIGGER_FALLING,
					"my_key_driver", NULL);

		if (ret < 0) {
				pr_err("request_irq  failed \n");
				return ret;
			}

		return 0;
}

static void __exit my_exit(void) {
		int i;
		free_irq(num, NULL);
		pr_info("a3 cleanup_module\n");for (i = 0; i < MY_MAX_MINORS; i++) {
		/* release devs[i] fields */
		cdev_del(&devs[i].cdev);
		}
		unregister_chrdev_region(mydev, MY_MAX_MINORS);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");
